﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DevExpress.Xpo;
using DevExpress.Persistent.Validation;

namespace Liekhus.TimeTracker.Module.UnitTests
{
    [TestClass]
    public class CoveyRoleTest
    {
        private Session m_session;

        [TestInitialize]
        public void InitializeTest()
        {
            m_session = new Session();
        }

        [TestMethod]
        public void CoveyRoleCreationTest()
        {
            DateTime start = DateTime.Now;
            CoveyRole role = new CoveyRole(m_session);
            Assert.IsTrue(role.CreatedOn >= start, "Covey Role Created On not being set correctly at initilization.");
        }

        [TestMethod]
        public void ValidCoveyRolePersistenceTest()
        {
            DateTime start = DateTime.Now;
            CoveyRole role = new CoveyRole(m_session);
            role.Code = "1";
            role.Name = "Role 1";

            role.Save();
            Assert.IsTrue(role.Code.Equals("1"), "Covey Role was modified on save.");
            Assert.IsTrue(role.Name.Equals("Role 1"), "Covey Role was modified on save.");
            Assert.IsTrue(role.CreatedOn >= start, "Covey Role Created On not being set corectly at initilization.");
            Assert.IsTrue(role.ModifiedOn >= start, "Covey Role Modified On not being set corectly on save.");
        }

        [TestMethod]
        public void InvalidCoveyRoleCodeTest()
        {
            CoveyRole role = new CoveyRole(m_session);
            role.Code = string.Empty;
            role.Name = "Role 1";

            RuleSet ruleSet = new RuleSet();
            RuleSetValidationResult validation = ruleSet.ValidateTarget(role, DefaultContexts.Save);

            Assert.IsTrue(validation.State == ValidationState.Invalid, "Expected to fail empty Covey role code check.");
        }

        [TestMethod]
        public void InvalidCoveyRoleNameTest()
        {
            CoveyRole role = new CoveyRole(m_session);
            role.Code = "1";
            role.Name = string.Empty;

            RuleSet ruleSet = new RuleSet();
            RuleSetValidationResult validation = ruleSet.ValidateTarget(role, DefaultContexts.Save);

            Assert.IsTrue(validation.State == ValidationState.Invalid, "Expected to fail empty Covey role name check.");
        }
    }
}
